package com.beeantec.jnrerc.modules.comment.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beeantec.jnrerc.core.api.ApiRest;
import com.beeantec.jnrerc.core.api.controller.BaseController;
import com.beeantec.jnrerc.entity.CommentLike;
import com.beeantec.jnrerc.entity.MemberLike;
import com.beeantec.jnrerc.modules.comment.service.CommentLikeService;
import com.beeantec.jnrerc.modules.comment.service.MemberLikeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api(value = "用户点赞", tags = "用户点赞")
@RestController
@RequestMapping(value = "/api/like")
public class LikeController extends BaseController {

    @Autowired
    private CommentLikeService commentLikeService;


    @Autowired
    private MemberLikeService memberLikeService;

    @ApiOperation(value = "添加评论点赞", notes = "添加评论接口")
    @RequestMapping(value = "/addComment" ,method = RequestMethod.POST)
    public ApiRest<String> add(@RequestBody CommentLike commentLike) {
        if (commentLikeService.saveOrUpdate(commentLike)){
            return success("成功");
        }
        return failure("失败");
    }


    @ApiOperation(value = "添加内容点赞或取消点赞", notes = "添加内容点赞或取消点赞")
    @RequestMapping(value = "/add" ,method = RequestMethod.POST)
    public ApiRest<String> add( @RequestBody MemberLike memberLike) {
        QueryWrapper<MemberLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MemberLike::getMemberId,memberLike.getMemberId())
                .eq(MemberLike::getItemId,memberLike.getItemId());
        if (memberLikeService.count(queryWrapper)>0){
            if (memberLikeService.remove(queryWrapper)){
                return success("成功");
            }
        }
        if (memberLikeService.saveOrUpdate(memberLike)){
            return success("成功");
        }
        return failure("失败");
    }





}
